package procilege.lang;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class BoolExprOr implements BoolExprElement,
		CompositeExpr<BoolExprElement> {

	private Set<BoolExprElement> terms = new HashSet<BoolExprElement>();

	public Set<BoolExprElement> getTerms() {
		return terms;
	}

	public Iterator<BoolExprElement> iterator() {
		return terms.iterator();
	}
	
	public void add(BoolExprElement elem) {
		or(elem);
	}
	
	public int size() {
		return terms.size();
	}

	public BoolExprOr() {
	}

	public BoolExprOr(Collection<BoolExprElement> terms) {

		for (BoolExprElement t : terms) {
			or(t);
		}
	}

	public void or(BoolExprElement term) {

		if (term == null)
			return;

		if (term instanceof BoolExprOr) {
			for (BoolExprElement b : ((BoolExprOr) term).getTerms()) {
				terms.add(b);
			}
		} else {
			terms.add(term);
		}
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((terms == null) ? 0 : terms.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final BoolExprOr other = (BoolExprOr) obj;
		if (terms == null) {
			if (other.terms != null)
				return false;
		} else if (!terms.equals(other.terms))
			return false;
		return true;
	}
}
